package com.edu.service.impl;

import com.edu.bean.User;
import com.edu.mapper.UserMapper;
import com.edu.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @Author:何包蛋
 * @Date :2022/11/10 - 11 - 10 - 15:36
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    @Override
    public int registUser(User user) {
        User user1 = userMapper.selectByName(user.getUsername());
        if (user1 == null){
            return userMapper.insertSelective(user);
        }else {
            return -1;
        }
    }

    @Override
    public int loginUser(String userName, String password) {
        User user = userMapper.selectByName(userName);
        if (user != null){
            if (user.getPassword().equals(password)){
                return 1;
            }else{
                return -1;
            }
        }else {
            return 0;
        }
    }

    @Override
    public int updatePassWord(String userName, String oldPass, String newPass) {
        User user = userMapper.selectByName(userName);
        if (user != null){
            if (user.getPassword().equals(oldPass)){
                User user1 = new User(user.getUserid(),userName,newPass);
                return userMapper.updateByPrimaryKey(user1);
            }else{
               return -1;
            }
        }else {
           return -2;
        }
    }
}
